import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { AuthenticationService } from 'my-web-core/auth';

export default class AuthenticationSlice {
  private static readonly slice = createSlice({
    name: 'authentication',
    initialState: {
      isAuthenticated: AuthenticationService.isAuthenticated,
      currentUser: AuthenticationService.currentUser,
      currentUsername: AuthenticationService.currentUsername,
    },
    reducers: {
      changeIsAuthenticated: (state, action: PayloadAction<any>) => {
        state.isAuthenticated = action.payload;
      },
      changeCurrentUser: (state, action: PayloadAction<any>) => {
        state.currentUser = action.payload;
      },
      changeCurrentUsername: (state, action: PayloadAction<any>) => {
        state.currentUsername = action.payload;
      },
    },
  });

  static readonly reducer = this.slice.reducer;
  static readonly changeIsAuthenticated = this.slice.actions.changeIsAuthenticated;
  static readonly changeCurrentUser = this.slice.actions.changeCurrentUser;
  static readonly changeCurrentUsername = this.slice.actions.changeCurrentUsername;
}
